package com.androidx.p013lv.mine.activity;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.bean.event.ChangeCityEvent;
import com.androidx.p013lv.base.dialog.ProgressBarDialog;
import com.androidx.p013lv.base.glide.GlideUtils;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.GlideEngine;
import com.androidx.p013lv.base.utils.PictureStyle;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.view.CircleImageView;
import com.androidx.p013lv.mine.C1207R;
import com.androidx.p013lv.mine.bean.EditInfoReq;
import com.androidx.p013lv.mine.bean.UploadImgBean;
import com.androidx.p013lv.mine.databinding.ActivityEditUserInfoLayoutBinding;
import com.androidx.p013lv.mine.model.EditUserModel;
import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.luck.picture.lib.permissions.PermissionChecker;
import com.luck.picture.lib.tools.PictureFileUtils;
import java.io.File;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import p000a.p002b.RxPermissions;
import p023io.reactivex.functions.Consumer;

/* renamed from: com.androidx.lv.mine.activity.EditUserInfoActivity */
public class EditUserInfoActivity extends BaseActivity<ActivityEditUserInfoLayoutBinding> {
    private EditInfoReq editInfoReq;
    private EditUserModel model;
    private PopupWindow popupWindow;
    private ProgressBarDialog progressBarDialog;

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityEditUserInfoLayoutBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C1207R.C1212layout.activity_edit_user_info_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        ((ActivityEditUserInfoLayoutBinding) this.binding).setCount(0);
        ((ActivityEditUserInfoLayoutBinding) this.binding).setClick(new OnClick());
        this.model = (EditUserModel) new ViewModelProvider(this).get(EditUserModel.class);
        setUserInfo();
        initPopupWindow();
        ((ActivityEditUserInfoLayoutBinding) this.binding).edNickName.addTextChangedListener(new TextWatcher() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.C12241 */

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void afterTextChanged(Editable editable) {
                EditUserInfoActivity.this.editInfoReq.setNickName(editable.toString());
            }
        });
        ((ActivityEditUserInfoLayoutBinding) this.binding).edIntroduce.addTextChangedListener(new TextWatcher() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.C12262 */

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                ((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).setCount(Integer.valueOf(charSequence.length()));
            }

            public void afterTextChanged(Editable editable) {
                EditUserInfoActivity.this.editInfoReq.setPersonSign(editable.toString());
            }
        });
        this.model.dataImg().observe(this, new Observer<BaseRes<UploadImgBean>>() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.C12273 */

            public void onChanged(BaseRes<UploadImgBean> baseRes) {
                if (baseRes.getCode() == 1000001) {
                    EditUserInfoActivity editUserInfoActivity = EditUserInfoActivity.this;
                    editUserInfoActivity.showProgressBarDialog("已經上傳：" + ((int) baseRes.getData().getProgressNum()) + "%，請耐心等待");
                } else if (baseRes.getCode() == 200) {
                    EditUserInfoActivity.this.showProgressBarDialog("正在更新資料，請稍後...");
                    EditUserInfoActivity.this.editInfoReq.setLogo(baseRes.getData().getFileName());
                    EditUserInfoActivity.this.model.modifyUserInfo(EditUserInfoActivity.this.editInfoReq);
                } else {
                    EditUserInfoActivity.this.dismissProgressBarDialog();
                    ToastUtils.getInstance().showWeak("上傳失敗，請重新選擇圖片上傳");
                }
            }
        });
        this.model.dataModify().observe(this, new Observer<BaseRes<UserInfo>>() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.C12284 */

            public void onChanged(BaseRes<UserInfo> baseRes) {
                EditUserInfoActivity.this.dismissProgressBarDialog();
                if (baseRes.getCode() == 200) {
                    UserInfo userInfo = SpUtils.getInstance().getUserInfo();
                    userInfo.setLogo(EditUserInfoActivity.this.editInfoReq.getLogo());
                    userInfo.setCityCode(EditUserInfoActivity.this.editInfoReq.getCode());
                    userInfo.setNickName(EditUserInfoActivity.this.editInfoReq.getNickName());
                    userInfo.setGender(EditUserInfoActivity.this.editInfoReq.getGender());
                    userInfo.setPersonSign(EditUserInfoActivity.this.editInfoReq.getPersonSign());
                    userInfo.setCityName(((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).getCityName());
                    userInfo.setProvinceName(baseRes.getData().getProvinceName());
                    SpUtils.getInstance().setUserInfo(userInfo);
                    ToastUtils.getInstance().showCorrect("修改成功");
                    EditUserInfoActivity.this.finish();
                    return;
                }
                ToastUtils.getInstance().showWeak(baseRes.getMsg());
            }
        });
    }

    public void setUserInfo() {
        this.editInfoReq = new EditInfoReq();
        UserInfo userInfo = SpUtils.getInstance().getUserInfo();
        this.editInfoReq.setNickName(userInfo.getNickName());
        this.editInfoReq.setPersonSign(userInfo.getPersonSign());
        this.editInfoReq.setGender(userInfo.getGender());
        this.editInfoReq.setLogo(userInfo.getLogo());
        this.editInfoReq.setCode(userInfo.getCityCode());
        ((ActivityEditUserInfoLayoutBinding) this.binding).setCover(SpUtils.getInstance().getString("domain") + userInfo.getLogo());
        ((ActivityEditUserInfoLayoutBinding) this.binding).setCityName(userInfo.getCityName());
        ((ActivityEditUserInfoLayoutBinding) this.binding).setInfo(this.editInfoReq);
    }

    private void initPopupWindow() {
        View inflate = View.inflate(this, C1207R.C1212layout.sex_select_popup_window, null);
        inflate.findViewById(C1207R.C1210id.tv_out_btn).setOnClickListener(new View.OnClickListener() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.View$OnClickListenerC12295 */

            public void onClick(View view) {
                EditUserInfoActivity.this.popupWindow.dismiss();
            }
        });
        inflate.findViewById(C1207R.C1210id.tv_man).setOnClickListener(new View.OnClickListener() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.View$OnClickListenerC12306 */

            public void onClick(View view) {
                EditUserInfoActivity.this.popupWindow.dismiss();
                ((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).tvSex.setText("男");
                EditUserInfoActivity.this.editInfoReq.setGender(2);
            }
        });
        inflate.findViewById(C1207R.C1210id.tv_girl).setOnClickListener(new View.OnClickListener() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.View$OnClickListenerC12317 */

            public void onClick(View view) {
                EditUserInfoActivity.this.popupWindow.dismiss();
                ((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).tvSex.setText("女");
                EditUserInfoActivity.this.editInfoReq.setGender(1);
            }
        });
        inflate.findViewById(C1207R.C1210id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.View$OnClickListenerC12328 */

            public void onClick(View view) {
                EditUserInfoActivity.this.popupWindow.dismiss();
            }
        });
        PopupWindow popupWindow2 = new PopupWindow(inflate, -1, -1);
        this.popupWindow = popupWindow2;
        popupWindow2.setOutsideTouchable(true);
        this.popupWindow.setBackgroundDrawable(new ColorDrawable());
        this.popupWindow.setAnimationStyle(C1207R.C1214style.PopAnimation);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String getNickName() {
        return ((ActivityEditUserInfoLayoutBinding) this.binding).edNickName.getText().toString().trim();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String getIntroduce() {
        return ((ActivityEditUserInfoLayoutBinding) this.binding).edIntroduce.getText().toString().trim();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void dismissProgressBarDialog() {
        ProgressBarDialog progressBarDialog2 = this.progressBarDialog;
        if (progressBarDialog2 != null && progressBarDialog2.isShowing()) {
            this.progressBarDialog.dismiss();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showProgressBarDialog(String str) {
        if (this.progressBarDialog == null) {
            this.progressBarDialog = new ProgressBarDialog(getActivity());
        }
        this.progressBarDialog.setHint(str);
        if (!this.progressBarDialog.isShowing()) {
            this.progressBarDialog.show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCityEventClick(ChangeCityEvent changeCityEvent) {
        if (changeCityEvent.getType() == 1) {
            ((ActivityEditUserInfoLayoutBinding) this.binding).setCityName(changeCityEvent.getCityEntity().getName());
            this.editInfoReq.setCode(changeCityEvent.getCityEntity().getCode());
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            new RxPermissions(this).request("android.permission.READ_EXTERNAL_STORAGE", "android.permission.CAMERA").subscribe(new Consumer<Boolean>() {
                /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.C12339 */

                public void accept(Boolean bool) throws Exception {
                    if (bool.booleanValue()) {
                        EditUserInfoActivity.this.selectPicture();
                    } else {
                        ToastUtils.getInstance().showSigh("未授權權限，請在設置中允許權限");
                    }
                }
            });
        } else {
            selectPicture();
        }
    }

    private void clearCache() {
        if (PermissionChecker.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE")) {
            PictureFileUtils.deleteAllCacheDirFile(this);
        } else {
            PermissionChecker.requestPermissions(this, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, 1);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void selectPicture() {
        PictureSelector.create(this).openGallery(PictureMimeType.ofImage()).imageEngine(GlideEngine.createGlideEngine()).setPictureStyle(PictureStyle.getInstance(this).getPictureParameterStyle()).setPictureCropStyle(PictureStyle.getInstance(this).getCropParameterStyle()).selectionMode(1).isPreviewImage(true).isCamera(true).isEnableCrop(true).isCompress(true).withAspectRatio(1, 1).freeStyleCropEnabled(true).circleDimmedLayer(true).showCropFrame(false).showCropGrid(false).minimumCompressSize(100).forResult(new OnResultCallbackListener<LocalMedia>() {
            /* class com.androidx.p013lv.mine.activity.EditUserInfoActivity.C122510 */

            @Override // com.luck.picture.lib.listener.OnResultCallbackListener
            public void onCancel() {
            }

            @Override // com.luck.picture.lib.listener.OnResultCallbackListener
            public void onResult(List<LocalMedia> list) {
                if (list != null && list.size() > 0) {
                    try {
                        LocalMedia localMedia = list.get(0);
                        EditUserInfoActivity.this.editInfoReq.setLogo(localMedia.getCutPath());
                        ((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).setInfo(EditUserInfoActivity.this.editInfoReq);
                        ((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).setCover(localMedia.getCutPath());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public static void loadCover(CircleImageView circleImageView, String str) {
        GlideUtils.loadPicHead(str, circleImageView);
    }

    public void hideSoftKeyboard(Activity activity) {
        View currentFocus = activity.getCurrentFocus();
        if (currentFocus != null) {
            ((InputMethodManager) activity.getSystemService("input_method")).hideSoftInputFromWindow(currentFocus.getWindowToken(), 2);
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        clearCache();
        EditUserModel editUserModel = this.model;
        if (editUserModel != null) {
            editUserModel.cancelHttp();
        }
    }

    /* renamed from: com.androidx.lv.mine.activity.EditUserInfoActivity$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            EditUserInfoActivity.this.finish();
        }

        public void uploadUserHead() {
            EditUserInfoActivity.this.requestPermission();
        }

        public void modifyInfo() {
            EditUserInfoActivity.this.editInfoReq.setNickName(EditUserInfoActivity.this.getNickName());
            EditUserInfoActivity.this.editInfoReq.setPersonSign(EditUserInfoActivity.this.getIntroduce());
            if (TextUtils.isEmpty(EditUserInfoActivity.this.editInfoReq.getNickName())) {
                ToastUtils.getInstance().showSigh("請輸入昵稱");
            } else if (EditUserInfoActivity.this.editInfoReq.getLogo() == null || EditUserInfoActivity.this.editInfoReq.getLogo().equals(SpUtils.getInstance().getUserInfo().getLogo())) {
                EditUserInfoActivity.this.model.modifyUserInfo(EditUserInfoActivity.this.editInfoReq);
            } else {
                EditUserInfoActivity.this.showProgressBarDialog("正在上傳，請稍後...");
                EditUserInfoActivity.this.model.updateAvatar(new File(EditUserInfoActivity.this.editInfoReq.getLogo()));
            }
        }

        public void editUserSex() {
            EditUserInfoActivity editUserInfoActivity = EditUserInfoActivity.this;
            editUserInfoActivity.hideSoftKeyboard(editUserInfoActivity);
            EditUserInfoActivity.this.popupWindow.showAtLocation(((ActivityEditUserInfoLayoutBinding) EditUserInfoActivity.this.binding).rootView, 17, 0, 0);
        }

        public void editUserCity() {
            ARouter.getInstance().build(Constance.CITY_SELECT_ACTIVITY).withInt("type", 1).navigation();
        }
    }
}
